package com.lc.rongzecaifu.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.rongzecaifu.adapter.FragPagerAdapter;
import com.lc.rongzecaifu.beans.MEventBeans;
import com.lc.rongzecaifu.fragment.DynamicFragment;
import com.lc.rongzecaifu.fragment.FirstFragment;
import com.lc.rongzecaifu.fragment.FourthFragment;
import com.lc.rongzecaifu.fragment.MineFragment;
import com.lc.rongzecaifu.rongzecaifu.R;
import com.lc.rongzecaifu.toast.LovelyToast;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;


public class MainActivity extends BaseFragmentActivity implements View.OnClickListener {

    /**
     * 主页viewpager的adapter
     */
    private FragPagerAdapter pagerAdapter;
    /**
     * 保存fragment的list
     */
    private ArrayList<Fragment> fragments;
    /**
     * fragment管理类
     */
    private FragmentManager fragmentManager;

    private FragmentTransaction ft;
    public Boolean FLAG = Boolean.TRUE;

    @Bind(R.id.iv_main_tab_01)
    ImageView iv_main_tab_01;
    @Bind(R.id.tv_main_tab_01)
    TextView tv_main_tab_01;
    @Bind(R.id.ll_main_tab_01)
    LinearLayout llMainTab01;
    @Bind(R.id.iv_main_tab_02)
    ImageView iv_main_tab_02;
    @Bind(R.id.tv_main_tab_02)
    TextView tv_main_tab_02;
    @Bind(R.id.ll_main_tab_02)
    LinearLayout llMainTab02;

    @Bind(R.id.iv_main_tab_04)
    ImageView iv_main_tab_04;
    @Bind(R.id.tv_main_tab_04)
    TextView tv_main_tab_04;
    @Bind(R.id.ll_main_tab_04)
    LinearLayout llMainTab04;
    @Bind(R.id.iv_main_tab_05)
    ImageView iv_main_tab_05;
    @Bind(R.id.tv_main_tab_05)
    TextView tv_main_tab_05;
    @Bind(R.id.ll_main_tab_05)
    LinearLayout llMainTab05;

    DynamicFragment secondFragment;

    FourthFragment borrowFragment;

    public String getAccessToken;

    private int mType; //这个是监控我的那页的type 当type==9 时候让它跳转富有注册

    @Bind(R.id.content)
    FrameLayout content;
    FirstFragment firstFragment;
    MineFragment mineFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
        setSelect(0);
       // initView();
        initListener();
        //  getData();
    }




    private void initListener() {
        llMainTab01.setOnClickListener(this);
        llMainTab02.setOnClickListener(this);
        //   llMainTab03.setOnClickListener(this);
        llMainTab04.setOnClickListener(this);
        llMainTab05.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_main_tab_01:
                // vpMain.setCurrentItem(0);
                setSelect(0);
                break;
            case R.id.ll_main_tab_02:
                setSelect(1);
                break;

            case R.id.ll_main_tab_04:
                setSelect(2);
                break;
            case R.id.ll_main_tab_05:
                setSelect(3);
                break;
        }
    }



    private void initFifthView() {
      //  ivMainTab05.setSelected(true);


        if (mType == 9) {

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    MEventBeans mEventBeans = new MEventBeans();
                    mEventBeans.type = 111;

                    EventBus.getDefault().post(mEventBeans);

                }
            }, 300);
        }

    }


    /**
     * 这里用到的了EventBus框架  博客教程:http://blog.csdn.net/lmj623565791/article/details/40920453
     *
     * @param type
     */
    public void onEventMainThread(Integer type) {
        Log.e("---", "EventBus收到int:" + type);
        //  Toast.makeText(this, "处理eventbus请求",0).show();
        if (type == 9) {

            mType = type;

        } else if (type == 1) {
            llMainTab02.performClick();
        }

    }

    private long firstTime = 0;


    /**
     * 连续两次推出键退出程序
     */
    private long atTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            Log.d("TIME", "当前时间:" + System.currentTimeMillis());

            if (System.currentTimeMillis() - atTime > 2000) {
                //  Toast.makeText(this, "再按一次退出应用程序", Toast.LENGTH_SHORT).show();


                LovelyToast.makeText(MainActivity.this, "再次点击确定退出", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);

                //  Toast.makeText(context, "asdf", Toast.LENGTH_SHORT).show();
                //   toast.setGravity(Gravity.CENTER, 0, 0);
                //  toast.show();
                atTime = System.currentTimeMillis();
            } else {
                LovelyToast.cancel();
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }





    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        reSetTab();
        hideFragment();
        switch (i) {
            case 0:
                //首页
                if (firstFragment == null) {
                    firstFragment = new FirstFragment();
                    ft.add(R.id.content, firstFragment);
                }
                iv_main_tab_01.setImageResource(R.mipmap.ic_main_one_selected);
               // tvHome.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                tv_main_tab_01.setTextColor(getResources().getColor(R.color.colorMainBlue));
                ft.show(firstFragment);
                break;
            case 1:
                //投资
                if (secondFragment == null) {
                    secondFragment = new DynamicFragment();
                    ft.add(R.id.content, secondFragment);
                }
                iv_main_tab_02.setImageResource(R.mipmap.ic_main_two_selected);
              //  tvTouzi.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                tv_main_tab_02.setTextColor(getResources().getColor(R.color.colorMainBlue));
                ft.show(secondFragment);
                break;
            case 2:
                //资产
                if (borrowFragment == null) {
                    borrowFragment = new FourthFragment();
                    ft.add(R.id.content, borrowFragment);
                }
                iv_main_tab_04.setImageResource(R.mipmap.ic_main_four_selected);
                tv_main_tab_04.setTextColor(getResources().getColor(R.color.colorMainBlue));
               // tvMe.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                ft.show(borrowFragment);
                break;
            case 3:
                //更多
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    ft.add(R.id.content, mineFragment);
                }
                iv_main_tab_05.setImageResource(R.mipmap.ic_main_five_selected);
               // tvMore.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                tv_main_tab_05.setTextColor(getResources().getColor(R.color.colorMainBlue));
                ft.show(mineFragment);
                break;
        }
        ft.commit();
    }

    private void reSetTab() {
        iv_main_tab_01.setImageResource(R.mipmap.ic_main_one_unselected);
        iv_main_tab_02.setImageResource(R.mipmap.ic_main_two_unselected);
        iv_main_tab_04.setImageResource(R.mipmap.ic_main_four_unselected);
        iv_main_tab_05.setImageResource(R.mipmap.ic_main_five_unselected);
        tv_main_tab_02.setTextColor(getResources().getColor(R.color.colorTextGrey));
        tv_main_tab_01.setTextColor(getResources().getColor(R.color.colorTextGrey));
        tv_main_tab_04.setTextColor(getResources().getColor(R.color.colorTextGrey));
        tv_main_tab_05.setTextColor(getResources().getColor(R.color.colorTextGrey));
        /*tvHome.setTextColor(UIUtils.getColor(R.color.home_back_unselected));
        tvTouzi.setTextColor(UIUtils.getColor(R.color.home_back_unselected));
        tvMe.setTextColor(UIUtils.getColor(R.color.home_back_unselected));
        tvMore.setTextColor(UIUtils.getColor(R.color.home_back_unselected));*/
    }


    private void hideFragment() {
        if (firstFragment != null) {
            ft.hide(firstFragment);
        }
        if (secondFragment != null) {
            ft.hide(secondFragment);
        }
        if (borrowFragment != null) {
            ft.hide(borrowFragment);
        }
        if (mineFragment != null) {
            ft.hide(mineFragment);
        }
    }

}
